package com.it.controller;


import com.it.domain.BlogTagEntity;
import com.it.service.BlogService;
import com.it.service.BlogTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;

/**
 * @author : code1997
 * @date : 2021/9/9 22:46
 */
@Controller
public class TagShowController {

    @Autowired
    private BlogTagService blogTagService;
    @Autowired
    private BlogService blogService;

    @GetMapping({"tags/{id}"})
    public String helloController2(@PathVariable Long id, Model model) {
        //相当于查询全表
        List<BlogTagEntity> blogTagEntities = blogTagService.listTagTop(10000);
        if (id == -1) {
            BlogTagEntity blogTagEntity = blogTagEntities.get(0);
            /*避免没有类型导致出现空指针异常的现象*/
            if (blogTagEntity != null) {
                id = blogTagEntity.getId();
            }
        }
        model.addAttribute("tags", blogTagEntities);
        model.addAttribute("page", blogService.listBlogByTagId(new HashMap<>()));
        model.addAttribute("activeTagId", id);
        return "tags";
    }
}
